package com.cgbl.cg.ui.course

import android.graphics.Typeface
import android.os.Bundle
import android.text.TextUtils
import android.util.TypedValue
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.databinding.AppActivityCourseMenuBinding
import com.cgbl.cg.view.SelectCoursePop
import com.cgbl.cg.vm.course.CourseMenuActViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.util.DisplayHelper

@Route(path = AppConstants.Router.COURSE.A_COURSE_MENU)
class CourseMenuActivity : GLBaseActivity<AppActivityCourseMenuBinding, CourseMenuActViewModel>() {
    private var typeTab = 0

    private var firstMenuPosition = 0

    private var secondMenuPosition = 0;

    private var mTitles = mutableListOf<String>()

    private var fragmentList = mutableListOf<Fragment>()

    private var allTypeList = mutableListOf<CourseAllTypeBean>()
    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_course_menu

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        for (type in allTypeList[firstMenuPosition].childrenList!!) {
            mTitles.add(type.name ?: "")
            fragmentList.add(CourseMenuFragment.newInstance(type.id + ""))
        }
        if (fragmentList.isNotEmpty())
            setTab()
    }

    private fun setTab() {
        binding.mViewPager2.adapter = object : FragmentStatePagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment = fragmentList[position]
            override fun getCount(): Int = fragmentList.size
        }
        binding.mViewPager2.offscreenPageLimit = fragmentList.size
        binding.tabLayout.setViewPager(binding.mViewPager2, mTitles.toTypedArray())
        binding.mViewPager2.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int, positionOffset: Float, positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                typeTab = position
                setTabSize(position)
            }

        })
    }

    override fun initData() {
        super.initData()
        viewModel.initData(allTypeList, firstMenuPosition)
    }

    //select tab textsize
    private fun setTabSize(position: Int) {
        for (i in mTitles.indices) {
            if (binding.tabLayout.getTitleView(i) != null) {
                if (position == i) {
                    binding.tabLayout.getTitleView(i).setTextSize(
                        TypedValue.COMPLEX_UNIT_PX, DisplayHelper.dpToPx(16).toFloat()
                    )
                } else {
                    binding.tabLayout.getTitleView(i).setTextSize(
                        TypedValue.COMPLEX_UNIT_PX, DisplayHelper.dpToPx(14).toFloat()
                    )
                }
                binding.tabLayout.getTitleView(i).typeface = Typeface.DEFAULT_BOLD
            }
        }
    }

    override fun initParam() {
        val listJson = intent.getStringExtra(AppConstants.BundleKey.CLASSIFICATION_DATA) ?: ""
        firstMenuPosition =
            intent.getIntExtra(AppConstants.BundleKey.CLASSIFICATION_FIRST_POSITION, 0)
        secondMenuPosition =
            intent.getIntExtra(AppConstants.BundleKey.CLASSIFICATION_SECOND_POSITION, 0)
        if (!TextUtils.isEmpty(listJson)) {
            allTypeList.addAll(JSON.parseArray(listJson, CourseAllTypeBean::class.java))
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.selectType.observe(this) {
            val selectCoursePop =
                SelectCoursePop(this, viewModel.typeName.get() ?: "", viewModel.list)
            XPopup.Builder(this).atView(binding.clTitle).asCustom(selectCoursePop).show()
            selectCoursePop.setConfirmClickListener {
                selectCoursePop.dismiss()
                viewModel.typeName.set(it.name)
                viewModel.typeId = it.id ?: ""
                resetFragment(it.childrenList ?: listOf())
            }
        }
    }

    private fun resetFragment(data: List<CourseAllTypeBean>) {
        mTitles.clear()
        fragmentList.clear()
        typeTab = 0
        for (type in data) {
            mTitles.add(type.name ?: "")
            fragmentList.add(CourseMenuFragment.newInstance(type.id + ""))
        }
        setTab()
        binding.tabLayout.currentTab = typeTab
        setTabSize(typeTab)
    }
}